package com.liu.eduService.controller;


import com.liu.R;
import com.liu.eduService.entity.EduChapter;
import com.liu.eduService.entity.chapter.ChapterVo;
import com.liu.eduService.service.EduChapterService;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author LSJ
 * @since 2022-01-07
 */
@RestController
@RequestMapping("/eduService/chapter")
@CrossOrigin
public class EduChapterController {
    @Autowired
    private EduChapterService service;

    @GetMapping("getChapterBycourseId/{courseId}")
    // 根据课程的id查询
    public R getChapterBycourseId(
            @ApiParam(name = "courseId", value = "课程ID", required = true)
            @PathVariable String courseId){

        List<ChapterVo> chapterVoList = service.findChapterBycourseId(courseId);
        return R.ok().data("list",chapterVoList);
    }

    @GetMapping("getChapterByChapterId/{ChapterId}")
    // 根据课程的id查询
    public R getChapterByChapterId(
            @ApiParam(name = "ChapterId", value = "课程ID", required = true)
            @PathVariable String ChapterId){

        EduChapter chapter = service.getById(ChapterId);
        return R.ok().data("chapter",chapter);
    }
    @PostMapping("addChapter")
    // 添加章节  根据课程的 章节实体类
    public R addChapter(
            @RequestBody EduChapter eduChapter){
        boolean save = service.save(eduChapter);
        return save?R.ok():R.error();
    }

    @PostMapping("updateChapter")
    // 修改章节  根据课程的 章节实体类
    public R updateChapter(
            @RequestBody EduChapter eduChapter){
        boolean update = service.updateById(eduChapter);
        return update?R.ok():R.error();
    }

    @DeleteMapping("{ChapterId}")
    // 删除章节  根据课程的 章节Id
    public R deleteChapter(
            @PathVariable String ChapterId){
        boolean remove = service.removeByChapterId(ChapterId);
        if(remove){
            return R.ok();
        }else{
            return R.error().message("删除失败");
        }
    }

//    In the editor, press Alt+Shift+向上箭头/ Alt+Shift+向下箭头 to extend or shrink your current code selection.


    @DeleteMapping("{courseId}")
    public R deletechapterBycourseId(@PathVariable  String courseId){
        boolean delete = service.deletechapterBycourseId(courseId);
        return delete?R.ok():R.error();
    }


}

